/*
 *  serial.h - implementing serial port communication functions
 *  Copyright (c) 2012 Neolink Co.Ltd
 */

#ifndef __SERIAL_H
#define __SERIAL_H

#include "private.h"

#define serial_debug         (0)         /* printf switch */

enum {
	SERIAL_OK = 0,
	SERIAL_ERR = -1,
	SERIAL_ABORT = -2,
	SERIAL_CRCFAIL = -3,
	SERIAL_TIMEOUT = -4,
	SERIAL_EOF = -5,
	SERIAL_MASK = -6,
	SERIAL_FORMAT = -7,
};

/**
 * open a serial port
 * @retval SERIAL_OK opening successed
 * @retval SERIAL_ERR opening failed
 */
int serial_open (
	serial_t *pfd,                       /* [out] return handle of serial */
	const char *device,                  /* [in] device name */
	unsigned int baudrate,               /* [in] baudrate */
	unsigned int bits,                   /* [in] start bits */
	unsigned int stop,                   /* [in] stop bits */
	char parity                          /* [in] 'E'- even, 'O' - odd, 'N' - null */
	);

/**
 * close the serial port
 */
void serial_close(serial_t fd);

/**
 * read data with specified length from the serial port
 * @retval SERIAL_OK if expected number of bytes read
 * @retval SERIAL_TIMEOUT if timed-out during the read
 * @retval SERIAL_ERR if read failed
 */
int serial_read (
	serial_t fd,                         /* [in] the handle for the serial port */
	unsigned char *buffer,               /* [in] the data to be read */
	unsigned int *len,                   /* [in,out] input the length of the data, return received data length */
	int timeout                          /* [in] millseconds waiting to read */
	);

/**
 * write to serial port
 * @return SERIAL_OK or SERIAL_ERR
 */
int serial_write (
	serial_t fd,                         /* [in] the handle for the serial port */
	unsigned char *buffer,               /* [in] buffer the data to be written */
	unsigned int len                     /* [in] len the length of the data */
	);

/**
 * flush serial read buffer
 */
int serial_flush (
	serial_t fd,                         /* [in] the handle for the serial port */
	int timeout                          /* [in] timeout, millseconds waiting to read */
	);

/**
 * DTR / RTS control
 */
void serial_clr_dtr(serial_t fd);
void serial_set_dtr(serial_t fd);
void serial_clr_rts(serial_t fd);
void serial_set_rts(serial_t fd);


#endif /* __SERIAL_H */
